package com.ssh.api.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.entity.ActivitySuperMarket;
import com.ssh.manager.ActivityImageManager;
import com.ssh.manager.ActivityManager;
import com.ssh.manager.ActivityMarketManager;
@Controller
@ParentPackage("client")
@Scope("prototype")
@Results(value={@Result(name ="list",type="json",params={"root","jr","excludeProperties","data\\[\\d+\\]\\.brand\\.createDate,data\\[\\d+\\]\\.brand\\.del","excludeNullProperties","true"}),
@Result(name ="comapny",type="json",params={"root","jr","excludeNullProperties","true"}),
@Result(name ="superMarketList",type="json",params={"root","jr","excludeNullProperties","true"}),
@Result(name ="findActivityImages",type="json",params={"root","jr","excludeproperties","data\\[\\d+\\]\\.createDate," +
		"data\\[\\d+\\]\\.activitySuperMarket\\.activity\\.brand\\.content,data\\[\\d+\\]\\.activitySuperMarket\\.activity\\.brand\\.createDate,data\\[\\d+\\]\\.activitySuperMarket\\.activity\\.brand\\.del," +
		"data\\[\\d+\\]\\.activitySuperMarket\\.activity\\.createDate,data\\[\\d+\\]\\.activitySuperMarket\\.activity\\.startDate,data\\[\\d+\\]\\.activitySuperMarket\\.activity\\.endDate,data\\[\\d+\\]\\.activitySuperMarket\\.activity\\.del,data\\[\\d+\\]\\.activitySuperMarket\\.activity\\.createDate," +
		"data\\[\\d+\\]\\.activitySuperMarket\\.superMarket\\.company\\.content,data\\[\\d+\\]\\.activitySuperMarket\\.superMarket\\.company\\.createDate,data\\[\\d+\\]\\.activitySuperMarket\\.superMarket\\.company\\.del," +
		"data\\[\\d+\\]\\.activitySuperMarket\\.superMarket\\.content,data\\[\\d+\\]\\.activitySuperMarket\\.superMarket\\.createDate,data\\[\\d+\\]\\.activitySuperMarket\\.superMarket\\.del,data\\[\\d+\\]\\.activitySuperMarket\\.superMarket\\.tel," +
		"data\\[\\d+\\]\\.activitySuperMarket\\.createDate","excludeNullProperties","true"}),
@Result(name ="findActivityAndMarket",type="json",params={"root","jr","excludeNullProperties","true"}),
})

public class ActivityAction extends ActionSupport {
	private static final long serialVersionUID = 970773022382019911L;
	JsonResult jr=new JsonResult();
	private long activityId;
	private long companyId;
	

	@Resource(name="activityManagerImpl")
	private ActivityManager activityManager;
	
	@Resource(name="activityMarketManagerImpl")
	private ActivityMarketManager activityMarketManager;
	
	@Resource(name="activityImageManagerImpl")
	private ActivityImageManager activityImageManager;
	public String  list() {
		jr.setMethod("list");
		try{
	    	jr.setCode("0");
	    	jr.setData(activityManager.list());	
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "list";
	}
	
	public String  companyList() {
		jr.setMethod("companyList");
		try{
	    	jr.setCode("0");
	    	jr.setData(activityManager.companyList(activityId));	
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "comapny";
	}
	
	public String  superMarketList() {
		jr.setMethod("superMarketList");
		try{
	    	jr.setCode("0");
	    	jr.setData(activityManager.superMarketList(activityId, companyId));	
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "superMarketList";
	}

	
	public String  findActivityImages(){
		
		jr.setMethod("findActivityImages");
		try{
	    	jr.setCode("0");
	    	jr.setData(activityImageManager.findActivityImages());	
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "findActivityImages";
	}
	
public String  findActivityAndMarket(){
		
		jr.setMethod("findActivityAndMarket");
		try{
	    	jr.setCode("0");
	    	List<ActivitySuperMarket> listDataBase=activityMarketManager.findActivityAndMarket();
	    	List<Map<String, String>> list=new ArrayList<Map<String,String>>();
	    	for(ActivitySuperMarket activitySuperMarket : listDataBase){
	    		Map<String, String> map=new HashMap<String, String>();
	    		map.put("activityId", activitySuperMarket.getActivity().getId().toString());
	    		map.put("activitySubject", activitySuperMarket.getActivity().getSubject());
	    		map.put("activityStartDate", activitySuperMarket.getActivity().getStartDate().toString());
	    		map.put("activityendDate", activitySuperMarket.getActivity().getEndDate().toString());
	    		map.put("superMarketId", activitySuperMarket.getSuperMarket().getId().toString());
	    		map.put("superMarketName", activitySuperMarket.getSuperMarket().getName());
	    		map.put("companyId", activitySuperMarket.getSuperMarket().getCompany().getId().toString());
	    		map.put("companyName", activitySuperMarket.getSuperMarket().getCompany().getName());
			list.add(map);	
	    	}
	    	jr.setData(list);	
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "findActivityAndMarket";
	}
	
	

	public JsonResult getJr() {
		return jr;
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	public void setActivityId(long activityId) {
		this.activityId = activityId;
	}

	public void setCompanyId(long companyId) {
		this.companyId = companyId;
	}

	
}
